@BeforeMethod এবং @AfterMethod এর কাজ

Java Technologies - টেস্টএনজি (TestNG) - TestNG এর বেসিক অ্যানোটেশন
217

@BeforeMethod এবং @AfterMethod হল TestNG-এর দুটি অ্যানোটেশন, যা টেস্ট মেথডের এক্সিকিউশনের আগে ও পরে নির্দিষ্ট কাজ সম্পাদনের জন্য ব্যবহৃত হয়। এগুলো প্রতিটি টেস্ট মেথডের সাথে আলাদাভাবে কাজ করে, যা টেস্টের জন্য সুনির্দিষ্ট সেটআপ এবং ক্লিনআপ নিশ্চিত করে।


@BeforeMethod

কাজ:
@BeforeMethod দিয়ে চিহ্নিত মেথডটি প্রতিটি টেস্ট মেথড চালানোর আগে স্বয়ংক্রিয়ভাবে একবার কার্যকর হয়। এটি সাধারণত টেস্ট মেথডের জন্য প্রয়োজনীয় সেটআপ বা ইনিশিয়ালাইজেশনের কাজ করে।

ব্যবহার:

  • ডাটাবেস সংযোগ স্থাপন।
  • ব্রাউজার লঞ্চ করা (সেলেনিয়ামের ক্ষেত্রে)।
  • টেস্ট ডেটা সেট আপ করা।

উদাহরণ:

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class BeforeMethodExample {

    @BeforeMethod
    public void setUp() {
        System.out.println("Setting up for the test method.");
    }

    @Test
    public void testMethodOne() {
        System.out.println("Executing Test Method One.");
    }

    @Test
    public void testMethodTwo() {
        System.out.println("Executing Test Method Two.");
    }
}

আউটপুট:

Setting up for the test method.
Executing Test Method One.
Setting up for the test method.
Executing Test Method Two.

উপরের আউটপুট থেকে দেখা যায়, প্রতিটি টেস্ট মেথড চালানোর আগে setUp মেথডটি কার্যকর হয়।


@AfterMethod

কাজ:
@AfterMethod দিয়ে চিহ্নিত মেথডটি প্রতিটি টেস্ট মেথড চালানোর পরে স্বয়ংক্রিয়ভাবে একবার কার্যকর হয়। এটি সাধারণত টেস্ট মেথডের পরে ক্লিনআপ বা রিসোর্স রিলিজের কাজ করে।

ব্যবহার:

  • ডাটাবেস সংযোগ বন্ধ করা।
  • ব্রাউজার বন্ধ করা।
  • টেস্ট ডেটা রিসেট করা।

উদাহরণ:

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class AfterMethodExample {

    @AfterMethod
    public void tearDown() {
        System.out.println("Cleaning up after the test method.");
    }

    @Test
    public void testMethodOne() {
        System.out.println("Executing Test Method One.");
    }

    @Test
    public void testMethodTwo() {
        System.out.println("Executing Test Method Two.");
    }
}

আউটপুট:

Executing Test Method One.
Cleaning up after the test method.
Executing Test Method Two.
Cleaning up after the test method.

উপরের আউটপুট থেকে দেখা যায়, প্রতিটি টেস্ট মেথড শেষ হওয়ার পরে tearDown মেথডটি কার্যকর হয়।


@BeforeMethod এবং @AfterMethod একসাথে ব্যবহার

উদাহরণ:

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class BeforeAfterExample {

    @BeforeMethod
    public void setUp() {
        System.out.println("BeforeMethod: Setting up resources.");
    }

    @Test
    public void testOne() {
        System.out.println("Test: Running Test One.");
    }

    @Test
    public void testTwo() {
        System.out.println("Test: Running Test Two.");
    }

    @AfterMethod
    public void tearDown() {
        System.out.println("AfterMethod: Releasing resources.");
    }
}

আউটপুট:

BeforeMethod: Setting up resources.
Test: Running Test One.
AfterMethod: Releasing resources.
BeforeMethod: Setting up resources.
Test: Running Test Two.
AfterMethod: Releasing resources.

সুবিধা

  1. কোড পুনরাবৃত্তি এড়ানো: টেস্ট মেথডের জন্য কমন সেটআপ এবং ক্লিনআপের কাজ এক জায়গায় রাখা যায়।
  2. স্বয়ংক্রিয় সম্পাদন: প্রতিটি টেস্ট মেথডের আগে ও পরে নির্ধারিত মেথড স্বয়ংক্রিয়ভাবে কার্যকর হয়, যা সময় বাঁচায়।
  3. টেস্ট স্ক্রিপ্ট মেইনটেনেবল: এটি টেস্ট স্ক্রিপ্টকে সহজ এবং গঠনমূলক রাখতে সাহায্য করে।

সারাংশ

@BeforeMethod প্রতিটি টেস্ট মেথড চালানোর আগে কাজ করে টেস্টের জন্য প্রস্তুতি নিশ্চিত করে, আর @AfterMethod প্রতিটি টেস্ট মেথড চালানোর পরে রিসোর্স মুক্ত করার কাজ করে। এই অ্যানোটেশনগুলো টেস্ট মেথডগুলোর নির্ভরযোগ্যতা এবং কার্যকারিতা বৃদ্ধি করে।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...